package com.ruoyi.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.business.domain.District;
import com.ruoyi.business.service.IDistrictService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 四级地域Controller
 *
 * @author MagicCube
 * @date 2023-05-04
 */
@RestController
@RequestMapping("/district")
public class DistrictController extends BaseController
{
    @Autowired
    private IDistrictService districtService;

    /**
     * 查询四级地域列表
     */
    @RequiresPermissions("business:district:list")
    @GetMapping("/list")
    public TableDataInfo list(District district)
    {
        startPage();
        List<District> list = districtService.selectDistrictList(district);
        return getDataTable(list);
    }
    /**
     * 查询省份城市列表
     */
    @GetMapping("/cityList")
    public AjaxResult provinceList(District district){
        if (district.getPid()==null||district.getPid()<0){
            district.setPid(0L);
        }
        List<District> list = districtService.selectDistrictList(district);
        return AjaxResult.success(list);
    }

    /**
     * 导出四级地域列表
     */
    @RequiresPermissions("business:district:export")
    @Log(title = "四级地域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, District district)
    {
        List<District> list = districtService.selectDistrictList(district);
        ExcelUtil<District> util = new ExcelUtil<District>(District.class);
        util.exportExcel(response, list, "四级地域数据");
    }

    /**
     * 获取四级地域详细信息
     */
    @RequiresPermissions("business:district:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(districtService.selectDistrictById(id));
    }

    /**
     * 新增四级地域
     */
    @RequiresPermissions("business:district:add")
    @Log(title = "四级地域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody District district)
    {
        return toAjax(districtService.insertDistrict(district));
    }

    /**
     * 修改四级地域
     */
    @RequiresPermissions("business:district:edit")
    @Log(title = "四级地域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody District district)
    {
        return toAjax(districtService.updateDistrict(district));
    }

    /**
     * 删除四级地域
     */
    @RequiresPermissions("business:district:remove")
    @Log(title = "四级地域", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(districtService.deleteDistrictByIds(ids));
    }
}
